package com.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.common.service.impl.CrudServiceImpl;
import com.mall.common.utils.ConvertUtils;
import com.mall.product.dao.BrandDao;
import com.mall.product.dao.CategoryBrandRelationDao;
import com.mall.product.dao.CategoryDao;
import com.mall.product.dto.BrandDTO;
import com.mall.product.dto.CategoryBrandRelationDTO;
import com.mall.product.entity.BrandEntity;
import com.mall.product.entity.CategoryBrandRelationEntity;
import com.mall.product.entity.CategoryEntity;
import com.mall.product.service.BrandService;
import com.mall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 品牌分类关联
 *
 * @author xjc xjc@163.com
 * @since 1.0.0 2022-07-17
 */
@Service
public class CategoryBrandRelationServiceImpl extends CrudServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity, CategoryBrandRelationDTO> implements CategoryBrandRelationService {

    @Resource
    private BrandDao brandDao;
    @Resource
    private CategoryDao categoryDao;

    @Resource
    BrandService brandService;

    @Override
    public QueryWrapper<CategoryBrandRelationEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Transactional
    @Override
    public void saveRelation(CategoryBrandRelationDTO dto) {
        Long brandId = dto.getBrandId();
        Long catelogId = dto.getCatelogId();

        BrandEntity brand = brandDao.selectById(brandId);
        CategoryEntity category = categoryDao.selectById(catelogId);

        dto.setBrandName(brand.getName());
        dto.setCatelogName(category.getName());
        this.save(dto);
    }

    @Override
    public List<CategoryBrandRelationDTO> getListByBrandId(Long brandId) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id",brandId);
        List<CategoryBrandRelationEntity> entityList = baseDao.selectList(wrapper);
        List<CategoryBrandRelationDTO> dtoList = ConvertUtils.sourceToTarget(entityList, CategoryBrandRelationDTO.class);
        return dtoList;
    }

    @Override
    public void updateBrandName(Long brandId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setBrandId(brandId);
        entity.setBrandName(name);
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id",brandId);
        baseDao.update(entity,wrapper);
    }

    @Override
    public void updateCategory(Long catId, String categoryName) {
        categoryDao.updateCategory(catId,categoryName);
    }

    @Override
    public List<BrandDTO> getListByCatId(Long catId) {

        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = baseDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        return categoryBrandRelationEntities.stream().map((entity) -> {
            BrandEntity brand = brandService.selectById(entity.getBrandId());
            return ConvertUtils.sourceToTarget(brand,BrandDTO.class);
        }).collect(Collectors.toList());
    }
}